package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.CountMapper;
import com.rivulet.foreign.mapper.ScholarshipMapper;
import com.rivulet.foreign.mapper.UserMapper;
import com.rivulet.foreign.mapper.VacationMapper;
import com.rivulet.foreign.pojo.Scholarship;
import com.rivulet.foreign.pojo.UserInformation;
import com.rivulet.foreign.pojo.Vacation;
import com.rivulet.foreign.service.BasicService;
import com.rivulet.foreign.service.ScholarshipService;
import com.rivulet.foreign.tools.TimeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * {通用功能}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Service
public class BasicServiceImpl implements BasicService {
    final
    ScholarshipMapper scholarshipMapper;
    final
    VacationMapper vacationMapper;
    final
    UserMapper userMapper;
    final
    CountMapper countMapper;

    public BasicServiceImpl(ScholarshipMapper scholarshipMapper, VacationMapper vacationMapper, UserMapper userMapper, CountMapper countMapper) {
        this.scholarshipMapper = scholarshipMapper;
        this.vacationMapper = vacationMapper;
        this.userMapper = userMapper;
        this.countMapper = countMapper;
    }

    /**
     * studentBasic
     */
    @Override
    public Map homeShowInformation(String student_id) {
        Map<String, String> map = new HashMap<>(10);
        map.put("student_id", student_id);
        try {
            Scholarship scholarship=scholarshipMapper.getScholarshipById(student_id);
            Vacation vacation = vacationMapper.getVacationById(student_id);
            String name = userMapper.getNameById(student_id);
            if (name != null) {
                map.put("student_id", name);
            }
            if (scholarship != null) {
                map.put("scholarship_type",scholarshipMapper.getScholarshipType(scholarship.getScholarship_type()));
                map.put("scholarship_result","已提交");
                if(scholarship.getIs_yes()==null){
                    map.put("scholarship_status","未审核");
                }else {
                    if(scholarship.getIs_yes()==1){
                        map.put("scholarship_status","审核通过！");
                    }else {
                        map.put("scholarship_status","驳回，驳回理由为："+scholarship.getAdvice());
                    }
                }
            }
            if (vacation != null) {
                map.put("vacation_begin_time", TimeTool.DateToString(vacation.getBegin_time()));
                map.put("vacation_end_time", TimeTool.DateToString(vacation.getEnd_time()));
                map.put("vacation_during_time", vacation.getDuration_time().toString() + "天");
                map.put("vacation_reason", vacation.getReason());
                map.put("vacation_destination", vacation.getDestination());
                map.put("vacation_status", "待审核");
                if (vacation.getIs_yes() == 1) {
                    map.put("vacation_status", "已通过");
                }
                if (vacation.getIs_yes() == 0) {
                    map.put("vacation_status", "已驳回");
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return map;
    }

    @Override
    public Map managerShowInformation(String student_id) {
        Map<String, Object> map = new HashMap<>(8);
        try {
            UserInformation userInformation = userMapper.getUserInformationById(student_id);
            if (userInformation.getChinese_name() != null) {
                map.put("student_id", userInformation.getChinese_name());
            } else {
                map.put("student_id", student_id);
            }
            map.put("user_information", userInformation);
            map.put("birth_date", TimeTool.DateToStringYMDTHM(userInformation.getBirth_date()));
            map.put("start_school_time", TimeTool.DateToStringYMDTHM(userInformation.getStart_school_time()));
            map.put("leave_school_time", TimeTool.DateToStringYMDTHM(userInformation.getLeave_school_time()));
            map.put("passport_end_time", TimeTool.DateToStringYMDTHM(userInformation.getPassport_end_time()));
            map.put("visa_end_time", TimeTool.DateToStringYMDTHM(userInformation.getVisa_end_time()));
            map.put("insurance_end_time", TimeTool.DateToStringYMDTHM(userInformation.getInsurance_end_time()));
        } catch (Exception e) {
            System.out.println("managerShowInformation" + e.toString());
        }
        return map;
    }

    @Override
    public Map studentBasic(String student_id){
        Map<String, String> map = new HashMap<>(1);

        String name = userMapper.getNameById(student_id);
        if (name != null) {
            map.put("student_id", name);
        } else {
            map.put("student_id", student_id);
        }
        return map;
    }

    /**
     * teacherBasic
     */
    @Override
    public Map tHome(String user_id) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("user_id", user_id);
        try {
            map.put("student_vacations", countMapper.vacationYet().toString());
            map.put("scholarship_number", countMapper.scholarshipNumber().toString());
            map.put("scholarship_pass_number", countMapper.scholarshipPassNumber().toString());
            map.put("scholarship_todo_number", countMapper.scholarshipToDo().toString());
        } catch (Exception e) {
           e.printStackTrace();
        }
        return map;
    }
    @Override
    public Map teacherBasicShow(String user_id) {
        Map<String, String> map = new HashMap<>(1);
        map.put("user_id",user_id);
        return map;
    }
}
